<template>
  <div class="bg flex x-center y-center">
    <el-card class="login-container">
      <div class="logo-container">
        <img src="@/assets/logo_long.png" width="140px">
      </div>

      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
      >
        <div class="title-container">
          <div class="title">管理端</div>
        </div>

        <el-form-item prop="username" label="账号">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password" label="密码">
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>

        <el-button
          :loading="loading"
          type="primary"
          @click.native="handleLogin"
        >登录</el-button>
        <br>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// import { login } from '@/api/user'
// const userApi = require('@/api/user.js')

export default {
  name: 'Login',
  data() {
    // 检查用户名
    const validateUsername = (rule, value, callback) => {
      if (value.trim().length === 0) {
        callback(new Error('用户名不能为空'))
      } else {
        callback()
      }
    }
    // 检查密码
    const validatePassword = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'teacher',
        password: 'test'
      },
      regForm: {
        name: '',
        pwd: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      regDialogVisable: false,
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },

  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },

  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },

    showRegPwd() {
      if (this.regPwdType === 'password') {
        this.regPwdType = ''
      } else {
        this.regPwdType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.regPwd.focus()
      })
    },

    /**
     * 处理登录事件
     */
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // dispatch表示调用的是异步方法，使用promise链返回结果和捕捉错误
          // 'user/login' 表示调用vuex的store中的user模块，login()方法
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scope>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #ffffff;
.bg {
  width: 100%;
  height: 100%;
  background-color: $bg;
}

.logo-container {
  text-align: center;
}

.login-container {
  .el-button {
    width: 100%;
    margin: 15px 0;
  }
}

$dark_gray: #889aa4;
$light_gray: rgb(78, 78, 78);

.login-container {
  border-radius: 11px;
  padding-top: 20px;
  margin-top: 0px;
  width: 450px;

  .login-form {
    width: 520px;
    max-width: 100%;
    padding: 20px 25px 22px;
    margin: 0 auto;
    overflow: hidden;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 21px;
      color: $light_gray;
      margin: 0px auto 10px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
